package com.jpcc.vw.onlineRadio.adapter.item;

import android.content.Intent;
import android.graphics.Color;
import android.os.RemoteException;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cns.android.atmospherelamp.AtmosphereLampManager;
import com.jpcc.vw.onlineRadio.R;
import com.jpcc.vw.onlineRadio.activity.AlbumDetailActivity;
import com.jpcc.vw.onlineRadio.activity.BoughtAlbumTrack;
import com.jpcc.vw.onlineRadio.activity.DownloadAlbumDetailActivity;
import com.jpcc.vw.onlineRadio.constants.Constants;
import com.jpcc.vw.onlineRadio.model.action.DeleteDownloadAlbumAction;
import com.jpcc.vw.onlineRadio.model.action.DeleteFavouriteAction;
import com.jpcc.vw.onlineRadio.model.action.RadioItemSelectedAction;
import com.jpcc.vw.onlineRadio.model.bean.CommonFavouritePaiedHistoryEntity;
import com.jpcc.vw.onlineRadio.recycle.RecyclerView.BaseItem;
import com.jpcc.vw.onlineRadio.service.AlbumRadioAudioPlayer;
import com.jpcc.vw.onlineRadio.widget.Indicator;

import de.greenrobot.event.EventBus;


public class CommonFavouritePaiedHistoryItem extends BaseItem<CommonFavouritePaiedHistoryEntity> implements View.OnClickListener {

    private ImageView imv_cover;
    private TextView tv_title;
    private TextView tv_simple_desc;
    private ImageView imv_delete;
    private TextView tv_item_count;
//    private Indicator imv_playing;
    private RelativeLayout layout_playing;
    private ImageView imv_is_vip_mark;
    private ImageView imv_play_status;
    private CommonFavouritePaiedHistoryEntity entity;

    public CommonFavouritePaiedHistoryItem(ViewGroup parent) {
        super(parent);
    }

    @Override
    public int getRes() {
        return R.layout.adapter_favourite_paied_history;
    }

    @Override
    public void onCreateView(View rootView) {
        imv_cover = (ImageView) rootView.findViewById(R.id.imv_cover);
        tv_title = (TextView) rootView.findViewById(R.id.tv_title);
        tv_item_count = (TextView) rootView.findViewById(R.id.tv_item_count);
//        tv_simple_desc=(TextView)rootView.findViewById(R.id.tv_simple_desc);
        imv_delete = (ImageView) rootView.findViewById(R.id.imv_delete);
//        imv_playing = (Indicator) rootView.findViewById(R.id.imv_playing);
        imv_play_status=(ImageView)rootView.findViewById(R.id.imv_play_status);
        layout_playing=(RelativeLayout)rootView.findViewById(R.id.layout_playing);
        imv_is_vip_mark=(ImageView)rootView.findViewById(R.id.imv_is_vip_mark);
    }

    @Override
    public void onBindView(CommonFavouritePaiedHistoryEntity commonFavouritePaiedHistoryEntity) {
        entity = commonFavouritePaiedHistoryEntity;
        tv_item_count.setVisibility(View.GONE);
        tv_item_count.setText(getPosition() + 1 + "");
        tv_title.setText(entity.getName());
        Glide.with(getContext()).load(entity.getCoverUrlSmall()).into(imv_cover);
        String bgColor = "#404040";
        if ((AlbumRadioAudioPlayer.get().getPlayMusic() != null) && "radio".equals(AlbumRadioAudioPlayer.get().getPlayMusic().getType())) {
            if (AlbumRadioAudioPlayer.get().getPlayMusic().getAlbumId() == entity.getId()) {
//                imv_playing.setVisibility(View.VISIBLE);
                imv_play_status.setVisibility(View.VISIBLE);
                if(AlbumRadioAudioPlayer.get().isPlaying()){
//                    imv_playing.setIndicatorPause(true);
                    imv_play_status.setImageResource(R.drawable.ic_play_in_list);
                }
                else{
//                    imv_playing.setIndicatorPause(false);
                    imv_play_status.setImageResource(R.drawable.ic_pause_in_list);
                }



                if (Constants.isRelease) {
                    try {
                        boolean atmosphereStatus = AtmosphereLampManager.getInstance(getContext()).isHmiAtmosphereLampOn();
                        String color = AtmosphereLampManager.getInstance(getContext()).getHmiAtmosphereLampColor();
                        if (atmosphereStatus) {
                            if (!TextUtils.isEmpty(color)) {
                                bgColor = color;
                            }
                        }
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
//                layout_playing.setBackgroundColor(Color.parseColor(bgColor));
                layout_playing.setBackgroundColor(Color.parseColor("#000000"));
            }
            else{
//                imv_playing.setVisibility(View.GONE);
                imv_play_status.setVisibility(View.GONE);
                layout_playing.setBackgroundColor(Color.parseColor("#000000"));
            }
        } else {
//            imv_playing.setVisibility(View.GONE);
            imv_play_status.setVisibility(View.GONE);
            layout_playing.setBackgroundColor(Color.parseColor("#000000"));
        }
        if (entity.getFrom() == 0 || 1 == entity.getFrom()) {
            imv_delete.setVisibility(View.VISIBLE);
            if (entity.getFrom() == 0) {
                imv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DeleteFavouriteAction action = new DeleteFavouriteAction();
                        action.setAlbumId(entity.getId());
                        action.setFrom(entity.getFrom());
                        EventBus.getDefault().post(action);
                    }
                });
            }
            //删除下载
            else if (entity.getFrom() == 1) {
                imv_delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DeleteDownloadAlbumAction action = new DeleteDownloadAlbumAction();
                        action.setAlbumId(entity.getId());
                        EventBus.getDefault().post(action);
                    }
                });
            }
        } else {
            imv_delete.setVisibility(View.INVISIBLE);
        }


        if(entity.getFrom()==0&&"album".equals(entity.getType())){
            if(entity.isVipFree()){
                imv_is_vip_mark.setVisibility(View.VISIBLE);
            }
            else{
                imv_is_vip_mark.setVisibility(View.INVISIBLE);
            }
        }
        else{
            imv_is_vip_mark.setVisibility(View.INVISIBLE);
        }

        if(AlbumRadioAudioPlayer.get().isPlaying()||AlbumRadioAudioPlayer.get().isPreparing()){
//            imv_playing.setIndicatorPause(true);
            imv_play_status.setImageResource(R.drawable.ic_play_in_list);
        }
        else{
//            imv_playing.setIndicatorPause(false);
            imv_play_status.setImageResource(R.drawable.ic_pause_in_list);
        }

        if(entity.getFrom()==2){
            if(entity.isVipFree()){
                imv_is_vip_mark.setVisibility(View.VISIBLE);
            }
            else{
                imv_is_vip_mark.setVisibility(View.GONE);
            }
        }
        else{
            imv_is_vip_mark.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        if ("album".equals(entity.getType())) {
            if (entity.getFrom() == 1) {
                Intent intent = new Intent(getContext(), DownloadAlbumDetailActivity.class);
                intent.putExtra("key_album_id", entity.getId());
                getContext().startActivity(intent);
            } else if (entity.getFrom() == 2) {
                Intent intent = new Intent(getContext(), AlbumDetailActivity.class);
                intent.putExtra("key_album_id", entity.getId());
                intent.putExtra("key_is_radio", false);
                intent.putExtra("key_from", entity.getFrom());
                getContext().startActivity(intent);
            } else {
                Intent intent = new Intent(getContext(), AlbumDetailActivity.class);
                intent.putExtra("key_album_id", entity.getId());
                intent.putExtra("key_is_radio", false);
                intent.putExtra("key_from", entity.getFrom());
                intent.putExtra("isvipFree",entity.isVipFree());
                intent.putExtra("isSample",entity.isSample());
                getContext().startActivity(intent);
            }
        } else if ("radio".equals(entity.getType())) {
            RadioItemSelectedAction action = new RadioItemSelectedAction();
            action.setRadioId(entity.getId());
            action.setFrom(entity.getFrom());
            action.setRadioName(entity.getName());
            EventBus.getDefault().post(action);
        }
    }
}
